package com.woniu40.separate_music.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.separate_music.entity.Music;
import com.woniu40.separate_music.entity.MusicType;
import com.woniu40.separate_music.entity.User;
import com.woniu40.separate_music.result.ResponseResult;
import com.woniu40.separate_music.entity.Music;
import com.woniu40.separate_music.result.ResponseResult;
import com.woniu40.separate_music.service.MusicService;
import com.woniu40.separate_music.service.MusicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    MusicService musicService ;
    @Autowired
    MusicTypeService musicTypeService;

    @PostMapping("/cancelCol/{userid}/{mid}")
    public ResponseResult<Integer> cancelCol(@PathVariable("userid")Integer userid ,
                                             @PathVariable("mid")Integer mid){
        ResponseResult<Integer> result = new ResponseResult<>();
        int cancelCol = musicService.cancelCol(userid, mid);
        result.setCode(cancelCol);
        if(cancelCol==200){
            result.setMessage("取消收藏成功！");
        }else{
            result.setMessage("取消收藏失败！");
        }
        return result;
    }

    @RequestMapping("/mlist")
    public ResponseResult<PageInfo<Music>> list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize",defaultValue = "2") Integer pageSize, String singer,
                                                @RequestParam(value = "tid",defaultValue = "0") Integer tid) {
        try {
            List<MusicType> typeAll = musicTypeService.findTypeAll();
            ResponseResult<List<MusicType>> typeresult = new ResponseResult<>();
            typeresult.setCode(200);
            typeresult.setMessage("查询类型音乐成功！！！");
            typeresult.setData(typeAll);
            PageInfo<Music> pageInfo = musicService.findMusicByPage(pageNum, pageSize, singer, tid);
            ResponseResult<PageInfo<Music>> result = new ResponseResult<>();

            result.setCode(200);
            result.setMessage("查询音乐成功！！！");
            result.setData(pageInfo);


            return result;
        } catch (RuntimeException e) {
            e.printStackTrace();
            ResponseResult<PageInfo<Music>> result = new ResponseResult<>();
            result.setCode(104);
            result.setMessage("查询失败");
            return result;
        }
    }

    @GetMapping("/mycol")
    public ResponseResult myCol(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "2")Integer pageSize,
                                Integer id){

        PageInfo<Music> musicByUid = musicService.findMusicByUid(pageNum, pageSize, id);
        ResponseResult<PageInfo<Music>> result = new ResponseResult<>();
        result.setCode(200);
        result.setMessage("查询成功！！！");
        result.setData(musicByUid);
        return result;
    }

    @DeleteMapping("/remove/{musicid}")
    public ResponseResult<Integer> removeMusic(@PathVariable(value = "musicid") Integer musicid){
        int i = musicService.removeMusic(musicid);
        ResponseResult<Integer> result = new ResponseResult<>();
        if(i==1){
            result.setCode(200);
            result.setMessage("删除成功!!!");
            result.setCode(i);
        }else {
            result.setCode(300);
            result.setMessage("删除失败...");
            result.setCode(i);
        }
        return result;
    }

    @PostMapping("/add")
    public ResponseResult<Music> addMusic(@RequestBody Music music){
        int i = musicService.addMusic(music);
        ResponseResult<Music> result = new ResponseResult<>();
        if(i==1){
            result.setCode(200);
            result.setMessage("添加成功");
            result.setData(music);
        }else {
            result.setCode(401);
            result.setMessage("添加失败...");
            result.setData(music);
        }
        return result;
    }

}
